VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "cThmbCache"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private mCol As cCollection

Private Sub Class_Initialize()
  Set mCol = New_c.Collection(False)
End Sub

Public Function SyncToRs(Rs As cRecordset, ByVal DesiredSize As Long) As Boolean
Dim i As Long, FileName As String, Srf As cCairoSurface, OrigW As Long, OrigH As Long
  If Rs Is Nothing Then SyncToRs = True: Exit Function
  
  For i = 0 To Rs.RecordCount - 1
    FileName = Rs.ValueMatrix(i, 4) & Rs.ValueMatrix(i, 3)
    If Not Exists(FileName) Then 'not yet cached
      Set Srf = GetSrfFromFile(FileName, DesiredSize, 32, OrigW, OrigH)
      
      mCol.Add Array(Srf, IIf(OrigW > 0 And OrigH > 0, OrigW & "x" & OrigH, "")), FileName
      If mCol.Count > 256 Then mCol.RemoveByIndex 0  'a cut-off at max 256 entries (oldest entries deleted first)
      Exit For 'as soon as we found and added an Image, we exit here (process only one entry per timer-interval)
    End If
  Next
  
  'final check, whether the Rs is now *completely* cached or not (we add only max one entry above, to keep the GUI responsive)
  For i = i To Rs.RecordCount - 1
    If Not Exists(Rs.ValueMatrix(i, 4) & Rs.ValueMatrix(i, 3)) Then Exit For
  Next
  SyncToRs = (i = Rs.RecordCount) 'when i=RecordCount, the whole Rs-Content is cached (the Rs holds the current visual Rows
End Function

Public Function GetSrfFromFile(FileName As String, ByVal DesiredSize As Long, ByVal DesiredIcoSize As Long, _
                               Optional OrigW As Long, Optional OrigH As Long) As cCairoSurface
  On Error Resume Next
    Select Case LCase$(Right$(FileName, 4))
      Case ".ico"
        OrigW = DesiredIcoSize: OrigH = DesiredIcoSize
        Set GetSrfFromFile = Cairo.ImageList.AddIconFromFile("Tmp", FileName, OrigW, OrigH)
        
      Case ".svg", "svgz", ".jpg", ".png", ".gif", ".bmp"
        Set GetSrfFromFile = Cairo.ImageList.AddImage("Tmp", FileName, DesiredSize, DesiredSize, True, OrigW, OrigH)
        
      Case Else 'all other files will get their FileType-Icon reported
        Set GetSrfFromFile = Cairo.ImageList.AddSurface("Tmp", New_c.FSO.GetFileIconSurface(FileName, False, IIf(DesiredIcoSize = 32, IconShellSize, IconLarge)))
    End Select
  If Err Then Err.Clear: Set GetSrfFromFile = Cairo.CreateSurface(1, 1)
End Function
 
Public Function Count() As Long
  Count = mCol.Count
End Function

Public Function Exists(ByVal FileName As String) As Boolean
  Exists = mCol.Exists(FileName)
End Function

Public Function ImgSrf(ByVal FileName As String) As cCairoSurface
  Set ImgSrf = mCol(FileName)(0)
End Function

Public Function ImgDim(ByVal FileName As String) As String
  ImgDim = mCol(FileName)(1)
End Function
